package com.example.user.mapper;

import com.example.user.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    // 推荐使用#{}取值，不要用${}，因为存在注入的风险
    @Insert("INSERT INTO user(name,phone,create_time,age) VALUES(#{name}, #{phone}, #{createTime},#{age})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") // 获取数据库自增id
    // keyProperty java对象的属性；keyColumn表示数据库的字段
    Long insert(User user);

    /**
     * 功能描述：查找全部
     */
    @Select("SELECT * FROM user")
    @Results({
            @Result(column = "create_time", property = "createTime")
            //javaType = java.util.Date.class
    })
    List<User> getAll();

    /**
     * 功能描述：根据id找对象
     */
    @Select("SELECT * FROM user WHERE id = #{id}")
    @Results({
            @Result(column = "create_time", property = "createTime")
    })
    User findById(Long id);

    /**
     * 功能描述：更新对象
     */
    @Update("UPDATE user SET name=#{name} WHERE id = #{id}")
    Integer updateById(User user);

    /**
     * 功能描述：根据id删除用户
     */
    @Delete("DELETE FROM user WHERE id = #{userId}")
    Integer deleteById(Long id);

    /**
     * 功能描述：批量删除用户
     */
    @Delete({"<script>","DELETE FROM user WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            " #{id} " +
            "</foreach>", "</script>"})
    Integer deleteByIds(@Param("ids") List<Long> ids);

}